summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SettingsSearchFragment.kt
blob: 55b6a0367be3f162370e4650569732389529f61d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.fragments

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.google.android.material.transition.MaterialSharedAxis
import info.debatty.java.stringsimilarity.Cosine
import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.databinding.FragmentSettingsSearchBinding
import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem
import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter
import org.yuzu.yuzu_emu.model.SettingsViewModel
import org.yuzu.yuzu_emu.utils.NativeConfig

class SettingsSearchFragment : Fragment() {
    private var _binding: FragmentSettingsSearchBinding? = null
    private val binding get() = _binding!!

    private var settingsAdapter: SettingsAdapter? = null

    private val settingsViewModel: SettingsViewModel by activityViewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, false)
        returnTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentSettingsSearchBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        settingsViewModel.setIsUsingSearch(true)

        if (savedInstanceState != null) {
            binding.searchText.setText(savedInstanceState.getString(SEARCH_TEXT))
        }

        settingsAdapter = SettingsAdapter(this, requireContext())

        val dividerDecoration = MaterialDividerItemDecoration(
            requireContext(),
            LinearLayoutManager.VERTICAL
        )
        dividerDecoration.isLastItemDecorated = false
        binding.settingsList.apply {
            adapter = settingsAdapter
            layoutManager = LinearLayoutManager(requireContext())
            addItemDecoration(dividerDecoration)
        }

        focusSearch()

        binding.backButton.setOnClickListener { settingsViewModel.setShouldNavigateBack(true) }
        binding.searchBackground.setOnClickListener { focusSearch() }
        binding.clearButton.setOnClickListener { binding.searchText.setText("") }
        binding.searchText.doOnTextChanged { _, _, _, _ ->
            search()
            binding.settingsList.smoothScrollToPosition(0)
        }
        settingsViewModel.shouldReloadSettingsList.observe(viewLifecycleOwner) {
            if (it) {
                settingsViewModel.setShouldReloadSettingsList(false)
                search()
            }
        }

        search()

        setInsets()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString(SEARCH_TEXT, binding.searchText.text.toString())
    }

    private fun search() {
        val searchTerm = binding.searchText.text.toString().lowercase()
        binding.clearButton.visibility =
            if (searchTerm.isEmpty()) View.INVISIBLE else View.VISIBLE
        if (searchTerm.isEmpty()) {
            binding.noResultsView.visibility = View.VISIBLE
            settingsAdapter?.submitList(emptyList())
            return
        }

        val baseList = SettingsItem.settingsItems
        val similarityAlgorithm = if (searchTerm.length > 2) Cosine() else Cosine(1)
        val sortedList: List<SettingsItem> = baseList.mapNotNull { item ->
            val title = getString(item.value.nameId).lowercase()
            val similarity = similarityAlgorithm.similarity(searchTerm, title)
            if (similarity > 0.08) {
                Pair(similarity, item)
            } else {
                null
            }
        }.sortedByDescending { it.first }.mapNotNull {
            val item = it.second.value
            val pairedSettingKey = item.setting.pairedSettingKey
            val optionalSetting: SettingsItem? = if (pairedSettingKey.isNotEmpty()) {
                val pairedSettingValue = NativeConfig.getBoolean(pairedSettingKey, false)
                if (pairedSettingValue) it.second.value else null
            } else {
                it.second.value
            }
            optionalSetting
        }
        settingsAdapter?.submitList(sortedList)
        binding.noResultsView.visibility =
            if (sortedList.isEmpty()) View.VISIBLE else View.INVISIBLE
    }

    private fun focusSearch() {
        binding.searchText.requestFocus()
        val imm = requireActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        imm?.showSoftInput(binding.searchText, InputMethodManager.SHOW_IMPLICIT)
    }

    private fun setInsets() =
        ViewCompat.setOnApplyWindowInsetsListener(
            binding.root
        ) { _: View, windowInsets: WindowInsetsCompat ->
            val extraListSpacing = resources.getDimensionPixelSize(R.dimen.spacing_med)
            val sideMargin = resources.getDimensionPixelSize(R.dimen.spacing_medlarge)
            val topMargin = resources.getDimensionPixelSize(R.dimen.spacing_chip)

            val barInsets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
            val cutoutInsets = windowInsets.getInsets(WindowInsetsCompat.Type.displayCutout())

            val leftInsets = barInsets.left + cutoutInsets.left
            val rightInsets = barInsets.right + cutoutInsets.right

            binding.settingsList.updatePadding(bottom = barInsets.bottom + extraListSpacing)
            binding.frameSearch.updatePadding(
                left = leftInsets + sideMargin,
                top = barInsets.top + topMargin,
                right = rightInsets + sideMargin
            )
            binding.noResultsView.updatePadding(
                left = leftInsets,
                right = rightInsets,
                bottom = barInsets.bottom
            )

            val mlpSettingsList = binding.settingsList.layoutParams as ViewGroup.MarginLayoutParams
            mlpSettingsList.leftMargin = leftInsets + sideMargin
            mlpSettingsList.rightMargin = rightInsets + sideMargin
            binding.settingsList.layoutParams = mlpSettingsList

            val mlpDivider = binding.divider.layoutParams as ViewGroup.MarginLayoutParams
            mlpDivider.leftMargin = leftInsets + sideMargin
            mlpDivider.rightMargin = rightInsets + sideMargin
            binding.divider.layoutParams = mlpDivider

            windowInsets
        }

    companion object {
        const val SEARCH_TEXT = "SearchText"
    }
}